pp108 : XSLT Architecture

XSLT Architecture

This topic describes the architecture of XSLT.

Architecture of XSLT

The XML file that needs to be transformed is converted into a NOM tree after being parsed through the NOM XML parser. Simultaneously, using the XPath parser, the XSLT parser generates an XSL object based on the XSL document provided. For more information on XPath parser, refer to XPath parser. At the point of transformation, the XSL object is used to transform the NOM tree into various formats such as HTML, text, or any other NOM tree.

Requirements of XSLT

XSLT requires the following:

  • A style sheet describing transformation rules
  • A transformation rule consists of a pattern and a template
  • A pattern is a configuration in the source tree
  • A template is a structure to be instantiated in the result tree

    To understand how XSLT operates, consider the example below:
    <xsl:template match="Title">
        <H1>
            <xsl:apply-templates/>
        </H1>
    </xsl:template>
    


    where, Input is<Title>Introduction</Title>, and Output is<H1>Introduction</H1>

XSLT Extensions


If the specialized elements of the XSLT namespace are not enough to perform the transformations you need, XSLT provides you with many ways to incorporate additional instruction elements and functions into your stylesheet. Using XSLT is in turn using the various elements from the XSLT namespace such as xsl:template, xsl:apply-templates, and xsl:output. XSLT elements such as xsl:apply-templates, xsl:text, and xsl:element that tell the XSLT processor to add something to the result tree, are called instructions. There are also 'top-level' elements such as xsl:output and xsl:strip-space that give more general instructions to the XSLT processor about how to perform the transformation.

As per the XSLT 1.0 specification, there are two types of extensions:

  • Elements
  • Functions

    You can define the extension element by defining namespace and including a value of 'extension-element-prefixes' attribute of stylesheet element or literal result element as the prefix. For function call, if the function name has a prefix and if it is mapped to a namespace, it is considered as an extension function.

NOM Specific Extension


Although the XSLT extensions help in transforming XML documents, they do not cater to all the transformation requirements. Therefore, while using Java to transform XML documents in a NOM environment, the NOM XSLT has one extension element, called js:script. Inside this element, you can write custom JavaScript functions and call it from XPath expression.

The following example shows a use of script:

<XSL:stylesheet 
    xmlns:XSL="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns1= "http://hello.com"
    xmlns:js = "http://www.cordys.com/XSL/script/javascript"
    extension-element-prefixes ="js"  version="1.0">
  <js:script>
     function todayStr()
     {
    var today = Date(); 
    return today.toString();
     }
  </js:script>
  <XSL:template match="/">
    <XSL:variable  name ="hi">
       <hello name ={js:todayStr()}>
       </hello>
    </XSL:variable >
    <XSL:apply-templates select = "//PurchaseOrder"/>     
  </XSL:template>
  <XSL:template match="PurchaseOrder" >
     <Invoice date = "{js:todayStr()}"/>
  </XSL:template > 
<XSL:stylesheet>


Note that script element should originate from the namespace http://www.cordys.com/XSL/script/javascript. Also note that the element-available() function will not return true for this element.

JavaScript


SpiderMonkey from Mozilla.org is being used as the JavaScript engine. You can pass String, Boolean, and Number as arguments to the JavaScript function. If you want to have a more complex manipulation that includes Nodeset, you must use the custom Java functions or native functions. XPath custom Java method call can return Nodeset however, the Nodeset must include only those nodes which are part of the input NOM tree originally provided. Also, there are no JavaScript NOM APIs for NOM.

Related reference

Using XSLT API

Related information

Overview of XSLT